package com.zk;

import java.io.IOException;

import org.apache.zookeeper.ZooKeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * zookeeper 的链接方法
 * 创建Zookeeper 
 * @author zzx
 *
 */
@Configuration
public class ZookeeperConfig{
	
	private Logger log = LoggerFactory.getLogger(ZookeeperConfig.class);
	
	
	@Value("${zookeeper.address}")
	private String zkUrl ;
	
	@Bean(name="zookeeper")
    public ZooKeeper zk(){
		log.info("************初始化zk************");
		ZooKeeper zooKeeper = null;
		
		try {
			zooKeeper = new ZooKeeper(zkUrl, 5000, new WatcherApi());
		} catch (IOException e) {
			log.info("链接失败",e);
			e.printStackTrace();
		}
		log.info("************初始化zk*** 结束*********");
		return zooKeeper;
    }
	
}
